﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Helper;

namespace KukaWii
{
   public partial class Form1 : Form
   {
      private List<KukaCoordData> coords;
      private Logger logger;
      private XMLReader reader;
      private XMLWriter writer;

      public Form1()
      {
         coords = new List<KukaCoordData>();
         logger = new Logger(Application.StartupPath +"\\logfile.txt");
         reader = new XMLReader();
         writer = new XMLWriter();

         InitializeComponent();

         tbSaveFilepath.Text = Application.StartupPath + "\\coords.xml";
         tbLoadFilepath.Text = Application.StartupPath + "\\coords.xml";

         timer1.Enabled = true;
         timer1.Interval = 500;
      }

      private void btSaveData_Click(object sender, EventArgs e)
      {
         writer.writeData(tbSaveFilepath.Text, coords);
      }

      private void btLoadData_Click(object sender, EventArgs e)
      {
         reader.readData(tbLoadFilepath.Text, ref coords);
      }

      private void timer1_Tick(object sender, EventArgs e)
      {
         Random r = new Random();

         labelA1.Text = (r.NextDouble() * 100).ToString().PadRight(16, '0');
         labelA2.Text = (r.NextDouble() * 100).ToString().PadRight(16, '0');
         labelA3.Text = (r.NextDouble() * 100).ToString().PadRight(16, '0');
         labelA4.Text = (r.NextDouble() * 100).ToString().PadRight(16, '0');
         labelA5.Text = (r.NextDouble() * 100).ToString().PadRight(16, '0');
         labelA6.Text = (r.NextDouble() * 100).ToString().PadRight(16, '0');
      }

      private void btLogPoint_Click(object sender, EventArgs e)
      {
         KukaCoordData tmp = new KukaCoordData();
         tmp.CoordType = KukaCoordType.AxisCoords;
         tmp.Coordinates.A1 = Convert.ToDouble(labelA1.Text);
         tmp.Coordinates.A2 = Convert.ToDouble(labelA2.Text);
         tmp.Coordinates.A3 = Convert.ToDouble(labelA3.Text);
         tmp.Coordinates.A4 = Convert.ToDouble(labelA4.Text);
         tmp.Coordinates.A5 = Convert.ToDouble(labelA5.Text);
         tmp.Coordinates.A6 = Convert.ToDouble(labelA6.Text);
         coords.Add(tmp);

         string addToRTB = tmp.CoordType.ToString() + "\n  "
                           + tmp.Coordinates.A1.ToString() + "\n  "
                           + tmp.Coordinates.A2.ToString() + "\n  "
                           + tmp.Coordinates.A3.ToString() + "\n  "
                           + tmp.Coordinates.A4.ToString() + "\n  "
                           + tmp.Coordinates.A5.ToString() + "\n  "
                           + tmp.Coordinates.A6.ToString() + "\n---------------------------------\n";

         rtbCoordinates.Text += addToRTB;
      }

   }
}
